/*
  用于生成饿了么表单的校验规则的工具库
*/

export const formValidator = {
  /**
   * 必填校验
   * @param {*} name
   * @param {*} trigger
   * @param {*} type
   * @returns
   */
  empty(name= "值", trigger = "blur", type = 'string') {
    return {
      required: true,
      trigger,
      message: (trigger==="blur"?"请输入":"请选择") + name,
      type
    };
  },

  nameCharacter(name = "值", trigger = "blur"){
    const validator = (rule,value,callback) => {
      const reg = /^[0-9a-zA-Z_\u4e00-\u9fa5]+$/
      if(value&&!reg.test(value)){
        return callback(new Error(`${name}只支持中文，字母，数字，下划线`))
      }
      callback()
    }
    return {
      validator,trigger
    }
  },

  number0(trigger = "blur"){
    const validator = (rule,value,callback) => {
      const reg = /^[0-9]\d*$/
      if(!reg.test(value)){
        return callback(new Error(`大于等于0的整数`))
      }
      callback()
    }
    return {
      validator,trigger
    }
  },

 number(trigger = "blur"){
   const validator = (rule,value,callback) => {
     const reg = /^[1-9]\d*$/
     if((value!=undefined && value!=='') && !reg.test(value)){
       return callback(new Error(`大于0的整数`))
     }
     callback()
   }
   return {
     validator,trigger
   }
  },

  number1(trigger = "blur"){
    const validator = (rule,value,callback) => {
      const reg = /^[1-9]\d+$|^([2-9])$/
      if((value!=undefined && value!=='') && !reg.test(value)){
        return callback(new Error(`大于1的整数`))
      }
      callback()
    }
    return {
      validator,trigger
    }
  },


  length(min,max){
    const validator = (rule,value,callback) => {
      if(String(value).length<min||String(value).length>max){
        return callback(new Error(`至少${min}位,最多${max}位`))
      }
      callback()
    }
    return {
      validator,trigger:'blur'
    }
  },

  password(){
    const validator = (rule,value,callback) => {
      const reg = /^(?![0-9]+$)(?![a-zA-Z]+$)(?![0-9a-z]+$)(?![0-9A-Z]+$)[0-9A-Za-z]{8,20}$/
      if(value && !reg.test(value)){
        return callback(new Error(`必须包含数字和大小写字母`))
      }
      callback()
    }
    return {
      validator,trigger:'blur'
    }
  },

  nonCharactor(){
    const validator = (rule,value,callback) => {
      const reg = /^[a-zA-Z0-9_\u4e00-\u9fa5]{0,}$/
      if(value && !reg.test(value)){
        return callback(new Error('不能包含特殊字符'))
      }
      callback()
    }
    return {
      validator,trigger:'blur'
    }
  },


  
}
